﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kaleida.ServiceMonitor.Model;
using Kaleida.ServiceMonitor.Model.Runtime;

namespace Kaleida.ServiceMonitor.UI.NotificationPanels
{
    [PanelName("Responses")]
    public partial class ResponsesNotificationPanel : UserControl, IModuleNotificationPanel
    {
        private Monitor monitor;
        private readonly Label lblMicroSummary;

        public ResponsesNotificationPanel()
        {
            InitializeComponent();

            lblMicroSummary = new Label {Visible = false, Dock = DockStyle.Fill};
            Controls.Add(lblMicroSummary);
        }

        public void Initialise(Monitor monitorInstance)
        {
            monitor = monitorInstance;
        }

        private LimitedList<RequestResult> DataSource
        {
			get { return chkErrorsOnly.Checked ? monitor.State.RecentFailures : monitor.State.RecentResponses; }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            DataSource.Clear();
        }

        public Control GetPanelControl()
        {
            return this;
        }

        public void RefreshContent()
        {
            lblDataSourceSummary.Text = PanelName + " - " + DataSource.Summary();

            var source = GetDataSourceListCopy();

            lock (lbRecentRequests)
            {
                SyncListBox(source);
            }

            lbRecentRequests.Invalidate();
            lblMicroSummary.Text = PanelName + " [Double-click to enlarge]";
        }

    	public string PanelName
    	{
			get { return "Responses - " + monitor.CompiledModule.Name; }
    	}


    	private void SyncListBox(IList<RequestResult> dataSource)
        {
            var superfluousListItems = lbRecentRequests.Items.Cast<RequestResult>().Where(i => !dataSource.Contains(i)).ToList();

            foreach (var superfluousListItem in superfluousListItems)
            {
                lbRecentRequests.Items.Remove(superfluousListItem);
            }

            var missingListItems = dataSource.Where(i => !lbRecentRequests.Items.Contains(i));
            lbRecentRequests.Items.AddRange(missingListItems.Cast<object>().ToArray());
        }

        private void btnCopyToClipboard_Click(object sender, EventArgs e)
        {
            var source = GetDataSourceListCopy();

            var content = new StringBuilder("SentOn\tDuration\r\n");

            foreach (RequestResult result in source)
            {
                content.AppendLine(result.ToClipboardItemText());
            }
            
            Clipboard.SetText(content.ToString(), TextDataFormat.Text);
        }

        private IList<RequestResult> GetDataSourceListCopy()
        {
            lock (DataSource)
            {
                return DataSource.GetItemsNewestFirst();
            }
        }

        private void ResponsesNotificationPanel_Resize(object sender, EventArgs e)
        {
            bool isMini = Width < 500 || Height < 150;
            bool isMicro = Width < 50 || Height < 40;

            topPanel.Visible = !isMini && !isMicro;
            lbRecentRequests.Location = isMini ? new Point(0, 0) : new Point(0, 26);
            lbRecentRequests.Size = isMini ? new Size(Width, Height) : new Size(Width, Height - 26);
            lbRecentRequests.Visible = !isMicro;
            lblMicroSummary.Visible = isMicro;
        }
    }
}
